﻿using Castle.Core.Logging;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebAppHappyNewYear.Filter
{
    public class CustomCacheResultFilterAttribute : Attribute, IResultFilter, IFilterMetadata, IOrderedFilter
    {

        public int Duration { get; set; }

        public int Order => 0;

        public void OnResultExecuted(ResultExecutedContext context)
        {
            //此方法执行时，已经制定了 Response，此时不能再操作 Response, 否则会出错
            Console.WriteLine($"{nameof(CustomCacheResultFilterAttribute)} OnResultExecuted");
        }

        public void OnResultExecuting(ResultExecutingContext context)
        {
            context.HttpContext.Response.Headers["Cache-Control"] = $"public,max-age={Duration}";
        }
    }


}
